<?php
namespace app\controller\admin;

use app\BaseController;
use app\Request;
use think\facade\Db;
use think\Collection;

class Member extends BaseController{
    public function index(Request $request)
    {
        $param = $request->param();
        $member = Db::name("scale_member");
        if(isset($param['username']) && !empty($param['username'])){
            $member = $member->whereLike('username', '%'.$param['username'].'%');
        }
        if(isset($param['mobile']) && !empty($param['mobile'])){
            $member = $member->whereLike('mobile', '%'.$param['mobile'].'%');
        }
        if(isset($param['status']) && $param['status'] !=''){
            $member = $member->where('status', $param['status']);
        }
        $member = $member->paginate($param['limit']);

        return api_json(20000,'请求数据成功',$member);
    }

    public function info(Request $request)
    {
        $param = $request->param();
        $member = Db::name("scale_member")->where('id',$param['id'])->select();
        return api_json(20000,'请求数据成功',$member);
    }

    public function changeStatus(Request $request)
    {
        $param = $request->param();
        $res = Db::name('scale_member')
            ->where('id',$param['id'])
            ->data(['status'=>$param['status']])
            ->update();
        if($res){
            return api_json(20000,'操作成功',[]);
        }else{
            return api_json(0,'操作失败',[]);
        }
    }

    public function delMember(Request $request)
    {
        $param = $request->param();
        $res = Db::name('scale_member')
            ->delete($param['id']);
        if($res){
            return api_json(20000,'操作成功',[]);
        }else{
            return api_json(0,'操作失败',[]);
        }
    }
    public function create(Request $request)
    {
        $param = $request->param();
        $data = [
          "username" => $param['username'],
          "mobile" => $param['mobile'],
          "introduction" => $param['introduction'],
          "status" => $param['status'],
        ];

        $res = Db::name('scale_member')
            ->save($data);
        if($res){
            return api_json(20000,'保存成功',[]);
        }else{
            return api_json(0,'保存失败',[]);
        }
    }
    public function update(Request $request)
    {
        $param = $request->param();
        $data = [
          "id" => $param['id'],
          "username" => $param['username'],
          "mobile" => $param['mobile'],
          "introduction" => $param['introduction'],
          "status" => $param['status'],
        ];

        $res = Db::name('scale_member')
            ->save($data);
        if($res){
            return api_json(20000,'保存成功',[]);
        }else{
            return api_json(0,'保存失败',[]);
        }
    }
}